package com.wswy.wyjk.ui.main.choose;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.httpapi.api.annotation.ViewL;
import com.my.httpapi.api.baseUtils.Toast;
import com.my.httpapi.api.baseView.BaseActivity;
import com.tencent.mmkv.MMKV;
import com.wswy.wyjk.AppContext;
import com.wswy.wyjk.R;
import com.wswy.wyjk.api.MMKVConstant;
import com.wswy.wyjk.core.App;
import com.wswy.wyjk.model.LicenseType;
import com.wswy.wyjk.model.LocalConfig;
import com.wswy.wyjk.model.PracticeContext;
import com.wswy.wyjk.ui.common.Router;
import com.wswy.wyjk.ui.main.choose.adapter.CarTypeAdapter;
import com.wswy.wyjk.ui.main.choose.model.CityEvent;
import com.wswy.wyjk.ui.main.event.CarType;
import com.wswy.wyjk.utils.CityUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

@ViewL(R.layout.activity_practice_type_new)
public class PracticeTypeNewActivity extends BaseActivity {

    private RelativeLayout rlChooseCity;
    private TextView tvCity, tvSubmit;
    private TextView tvPracticeTypeHint;
    private RecyclerView rvTypes;
    private CarTypeAdapter carTypeAdapter;

    private LocalConfig currentLocalConfig = new LocalConfig();

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();
        initData();
    }

    @Override
    protected String showTitle() {
        return null;
    }

    @Override
    protected boolean statusBarIsBlack() {
        return true;
    }

    @Override
    protected void destroy() {
        EventBus.getDefault().unregister(this);
    }


    private void initView() {
        rlChooseCity = findViewById(R.id.rl_choose_city);
        tvCity = findViewById(R.id.tv_city);
        tvSubmit = findViewById(R.id.tv_submit);
        tvPracticeTypeHint = findViewById(R.id.tv_practice_type_hint);
        rvTypes = findViewById(R.id.rv_types);
        initRecyclerView();
    }

    private void initData() {
        EventBus.getDefault().register(this);
        rlChooseCity.setOnClickListener(view -> Router.start(PracticeTypeNewActivity.this, Router.CHOOSE_CITY, null, null));
        int areaCode = 0;
        if (!TextUtils.isEmpty(App.getPracticeContext().getAreaCode())) {
            areaCode = Integer.valueOf(App.getPracticeContext().getAreaCode());
        }
        currentLocalConfig.setCityName(App.getPracticeContext().getCityName());
        currentLocalConfig.setAreaCode(areaCode);
        currentLocalConfig.setCarType(App.getPracticeContext().licenseType.getTypeName());
        tvCity.setText(App.getPracticeContext().getCityName());
        tvPracticeTypeHint.setText(getString(R.string.practice_type_hint, App.getPracticeContext().licenseType.getTitle()));

        List<LicenseType> listType = Arrays.asList(LicenseType.values());
        carTypeAdapter.setNewData(listType);
        carTypeAdapter.setTitle(App.getPracticeContext().licenseType.getTitle());
        carTypeAdapter.setOnItemClickListener((adapter, view, position) -> {
            String title = carTypeAdapter.getData().get(position).getTitle();
            carTypeAdapter.setTitle(title);
            tvPracticeTypeHint.setText(getString(R.string.practice_type_hint, title));
            currentLocalConfig.setCarType(carTypeAdapter.getData().get(position).getTypeName());
        });
        tvSubmit.setOnClickListener(view -> {
            if (TextUtils.isEmpty(currentLocalConfig.getCityName())) {
                Toast.show("请选择城市");
                return;
            }
            PracticeContext toMain = App.getPracticeContext();
            App.saveConfig(currentLocalConfig);

            if (toMain == null) {
                Router.start(PracticeTypeNewActivity.this, Router.MAIN, null, null);
            } else {
                EventBus.getDefault().post(new CarType(currentLocalConfig.getCarType()));
            }
            MMKV.defaultMMKV().encode(MMKVConstant.POSITION, 0);
            finish();
        });
    }

    private void initRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4, LinearLayoutManager.VERTICAL, false);
        carTypeAdapter = new CarTypeAdapter();
        rvTypes.setLayoutManager(gridLayoutManager);
        rvTypes.setAdapter(carTypeAdapter);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getCity(CityEvent cityEvent) {
        tvCity.setText(cityEvent.getCity());
        new CityUtils().allCity(cityEvent.getCity(), currentLocalConfig);
    }

}
